package com.yys.util;

import java.awt.*;
import java.awt.event.InputEvent;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Random;

public class yysUtil {


    static {
        System.setProperty("java.awt.headless", "false");
    }

    /**
     * 随机产生min~max之间的随机数
     *
     * @param min
     * @param max
     * @return
     */
    public static int randomarea(int min, int max) {
        Random random = new Random();
        int num = random.nextInt(max - min + 1) + min;
        return num;
    }


    /**
     * 初始化位置
     */
    public static Color go(int x, int y) {
        try {
            Robot robot = new Robot();
            robot.delay(2000);//等待2秒再操作
            robot.mouseMove(x, y);
            Color pixelColor = robot.getPixelColor(x, y);
            return pixelColor;
        } catch (AWTException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 鼠标移动到（x,y）并单击左键
     *
     * @param x
     * @param y
     */
    public static void start(int x, int y) {
        try {
            Robot robot = new Robot();
            robot.delay(randomarea(200, 500));
        } catch (AWTException e) {
            e.printStackTrace();
        }
        moveAndClick(x, y);

    }

    /**
     * 等待一次挖土结束
     *
     * @param time
     */
    public static void wait(int time, int x, int y) {
        try {
            Robot robot = new Robot();
            time = time + 9;//加载准备时间
            time = new Random().nextInt((time + 1) * 1000 - (time * 1000) + 1) + (time * 1000);
            robot.delay(time);//等待结束随机1秒之内
            int ri = new Random().nextInt(6 - 5 + 1) + 5;
            for (int i = 1; i <= ri; i++) {//随机点5~6次回到副本门口/组队界面
                moveAndClick(x, y);
            }

        } catch (AWTException e) {
            e.printStackTrace();
        }


    }

    /**
     * 单人单击次数减少
     * @param time
     * @param x
     * @param y
     */
    public static void wait2(int time, int x, int y) {
        try {
            Robot robot = new Robot();
            time = time + 9;//加载准备时间
            time = new Random().nextInt((time + 1) * 1000 - (time * 1000) + 1) + (time * 1000);
            robot.delay(time);//等待结束随机1秒之内
            int ri = randomarea(1,2);
            for (int i = 1; i <= ri; i++) {//随机点5~6次回到副本门口/组队界面
                moveAndClick(x, y);
            }

        } catch (AWTException e) {
            e.printStackTrace();
        }


    }





    /**
     * 移动到(x,y)位置并点击
     *
     * @param x
     * @param y
     */
    public static void moveAndClick(int x, int y) {//
        try {
            Robot robot = new Robot();
            robot.mouseMove(randomarea((x - 5), (x + 5)), randomarea((y - 5), (y + 5)));//在x,y+-50位置随机
            robot.delay(new Random().nextInt(400 - 300 + 1) + 300);//随机400~300毫秒之内点击
            robot.mousePress(InputEvent.BUTTON1_MASK);//单击左键
            robot.delay(randomarea(100, 300));//30~50毫秒
            robot.mouseRelease(InputEvent.BUTTON1_MASK);

        } catch (AWTException e) {
            e.printStackTrace();
        }


    }

    public static void restart() throws IOException {
//用一条指定的命令去构造一个进程生成器
        ProcessBuilder pb=new ProcessBuilder("java","-jar","yys_1.0.0.jar");
//让这个进程的工作区空间改为F:\dist
//这样的话,它就会去F:\dist目录下找Test.jar这个文件
        pb.directory(new File("C:\\"));
//得到进程生成器的环境 变量,这个变量我们可以改,
//改了以后也会反应到新起的进程里面去
        Map<String,String> map=pb.environment();
        Process p=pb.start();
//然后就可以对p做自己想做的事情了
//自己这个时候就可以退出了
        System.out.println("重新启动成功!");
        System.exit(0);
    }


    public static void logtime(String log){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(dateFormat.format(new Date())+log);
    }




}
